﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic.Devices;
using SaleOfMoviesSystem.Xml.Enum;
using SaleOfMoviesSystem.Xml.Models;
using SaleOfMoviesSystem.Xml.Services;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;

namespace SaleOfMoviesSystem.Xml
{
    public partial class FrmMain : Form
    {
        public decimal Price = 0;

        public FrmMain()
        {
            InitializeComponent();
        }

        public MovieServices MovieServices { get; set; }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            MovieServices = new MovieServices();
            foreach (var movie in MovieServices.Movies)
            {
                var movieNode = new TreeNode(movie.Name);
                movieNode.Tag = movie;
                treeView1.Nodes.Add(movieNode);
                foreach (var time in movie.MovieTimes)
                {
                    var timeNode = new TreeNode(time.Time);
                    timeNode.Tag = time;
                    movieNode.Nodes.Add(timeNode);
                }
            }

            treeView1.AfterSelect += treeView1_AfterSelect;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 1)
            {
                var movie = e.Node.Parent.Tag as Movie;
                pictureBox1.Image = movie.Img;
                label1.Text = movie.Name;
                label2.Text = movie.Director;
                label3.Text = movie.MainPlayer;
                label4.Text = movie.Price.ToString();
                Price = movie.Price;
                //C# Winform label自定义大小与居中，文字自动换行，背景透明和导入图片: http://www.liangshunet.com/ca/201406/947425193.htm
                label5.Text = movie.Description;
                //textBox1.Text = movie.Description;

                comboBox1.DataSource = System.Enum.GetNames(typeof(MovieTicketTypeEnums));
                //comboBox1.Text = "学生票";
                //switch ((MovieTicketTypeEnums)System.Enum.Parse(typeof(MovieTicketTypeEnums), comboBox1.Text))
                //{
                //    case MovieTicketTypeEnums.学生票:
                //        label4.Text = "￥ " + (movie.Price * (decimal)0.8).ToString();
                //        break;
                //        ;

                //    case MovieTicketTypeEnums.赠票:
                //        label4.Text = "￥ 0";
                //        break;

                //    case MovieTicketTypeEnums.全票:
                //        label4.Text = "￥ " + movie.Price.ToString();
                //        break;
                //        ;

                //    default:
                //        throw new Exception("没有选择购票类型");
                //}

                InitTicket((e.Node.Tag as MovieTime).Tickets);
            }
            //throw new NotImplementedException();
        }

        private void InitTicket(List<Ticket> tickets)
        {
            flowLayoutPanel1.Controls.Clear();
            foreach (var ticket in tickets)
            {
                flowLayoutPanel1.Controls.Add(new UCTicket(ticket));
            }
        }

        //购买
        private void button1_Click(object sender, EventArgs e)
        {
            List<Ticket> tickets = new List<Ticket>();
            int ticketCount = 0;
            foreach (UCTicket ucTicket in flowLayoutPanel1.Controls)
            {
                if (ucTicket.BackColor == Color.Yellow)
                {
                    ticketCount += 1;
                    tickets.Add(ucTicket.Ticket);
                }
            }

            var movieName = treeView1.SelectedNode.Parent.Text;
            var timeName = treeView1.SelectedNode.Text;
            var result = MovieServices.BuyTickets(movieName, timeName, tickets.Select(m => m.TicketName).ToList());
            InitTicket((treeView1.SelectedNode.Tag as MovieTime).Tickets);
            label15.Text = (result[1] / result[0]).ToString();
            label16.Text = result[1].ToString();
            MessageBox.Show("购票成功！");
        }

        //选择购票类型
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Ticket> tickets = new List<Ticket>();
            var movieName = treeView1.SelectedNode.Parent.Text;
            var timeName = treeView1.SelectedNode.Text;
            label4.Text = MovieServices.BuyTickets(movieName, timeName, tickets.Select(m => m.TicketName).ToList(), (MovieTicketTypeEnums)System.Enum.Parse(typeof(MovieTicketTypeEnums), (string)comboBox1.Text))[0].ToString();
            //switch ((MovieTicketTypeEnums)System.Enum.Parse(typeof(MovieTicketTypeEnums), (string)comboBox1.Text))
            //{
            //    case MovieTicketTypeEnums.全票:
            //        //label4.Text = (decimal.Parse(label4.Text) * (decimal)1).ToString();
            //        label4.Text = Price.ToString();
            //        break;
            //        ;

            //    case MovieTicketTypeEnums.赠票:
            //        label4.Text = "0";
            //        break;

            //    case MovieTicketTypeEnums.学生票:
            //        //label4.Text = (decimal.Parse(label4.Text) * (decimal)0.8).ToString();
            //        label4.Text = (Price * (decimal)0.8).ToString();
            //        break;
            //        ;

            //    default:
            //        throw new Exception("没有选择购票类型");
            //}
        }

        //取消购票
        private void button2_Click(object sender, EventArgs e)
        {
            int ticketCount = 0;
            foreach (UCTicket ucTicket in flowLayoutPanel1.Controls)
            {
                if (ucTicket.BackColor == Color.Yellow)
                {
                    ticketCount += 1;
                    ucTicket.BackColor = SystemColors.Control;
                }
            }

            if (ticketCount == 0)
            {
                MessageBox.Show("您当前没有选中座位，无法进行取消操作！");
            }
        }

        //退票
        private void button3_Click(object sender, EventArgs e)
        {
            List<Ticket> tickets = new List<Ticket>();
            int ticketCount = 0;
            foreach (UCTicket ucTicket in flowLayoutPanel1.Controls)
            {
                if (ucTicket.BackColor == Color.Red)
                {
                    ticketCount += 1;
                    ucTicket.Ticket.IsSaled = false;
                    ucTicket.BackColor = SystemColors.Control;
                    tickets.Add(ucTicket.Ticket);
                }
            }
            var movieName = treeView1.SelectedNode.Parent.Text;
            var timeName = treeView1.SelectedNode.Text;
            MessageBox.Show($"您将推订 {ticketCount} 张 {comboBox1.Text} ,  退款金额为：" + MovieServices.BuyTickets(movieName, timeName, tickets.Select(m => m.TicketName).ToList(), (MovieTicketTypeEnums)System.Enum.Parse(typeof(MovieTicketTypeEnums), (string)comboBox1.Text))[1].ToString());
            var saledTickets = (treeView1.SelectedNode.Tag as MovieTime).Tickets;
            foreach (var ticket in saledTickets)
            {
                ticket.IsSaled = false;
            }

            InitTicket(saledTickets);
        }
    }
}